home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Eudora Stat Server / rvf-hack / reportrev2.pl < prev    next >
Encoding:
Perl Script  |  2001-06-23  |  1.7 KB  |  111 lines

  1. #!/usr/bin/perl
  2.  
  3. #
  4. # use CGI.pm and DBI external modules
  5. #
  6. # CGI.pm is part of standard perl distribution
  7. # DBI is available from www.cpan.org
  8. #
  9. # program uses CSV files to store data
  10. #
  11.  
  12. use CGI;
  13. use DBI;
  14. use CGI::Carp qw(fatalsToBrowser);
  15.  
  16. #
  17. # new instance of cgi
  18. #
  19.  
  20. my $q = new CGI;
  21.  
  22. sub print_table {
  23.  
  24. my $description = shift;
  25. my $variable = shift;
  26.  
  27. #
  28. # set up for database access
  29. # ref: O'Reilley CGI Programming with Perl
  30. #
  31.  
  32. my $dbdir = "/Library/WebServer/CGI-Executables";
  33. #my $dbdir = "C:/Documents and Settings/richard/My Documents/Projects/machack";
  34. my $dbh = DBI->connect("DBI:CSV:f_dir=$dbdir")
  35.         or die "Can't connect: " . $DBI::errstr;
  36.  
  37. my $sql = "select user, $variable, days from data order by user";
  38.  
  39. my $sth = $dbh->prepare($sql)
  40.         or die "Can't prepare: " . $dbh->errstr();
  41.  
  42. $sth->execute()
  43.         or die "Can not execute: " . $sth->errstr();
  44.  
  45. my @row;
  46.  
  47. print qq`
  48. <table border="1">
  49. <tr><td>User</td><td>$description</td></tr>
  50. `;
  51.  
  52. my $i = 0;
  53. my @list;
  54. my $msg_avg;
  55.  
  56. while (@row = $sth->fetchrow_array()){
  57.      $msg_avg = int $row[1] / $row[2];    
  58.      if ($variable eq "usage_avg_day"){
  59.         $msg_avg = int $msg_avg / 60;
  60. }    
  61.     @list[$i] = "<tr><td>" .$row[0] . "</td><td>" . $msg_avg . "</td></tr>\n";
  62.     print @list[$i];
  63.     $i++;
  64. }
  65.  
  66. print qq`
  67. </table>
  68. <br>
  69. `;
  70.  
  71. $sth->finish;
  72.  
  73. $dbh->disconnect;
  74. }
  75.  
  76. #
  77. # tell browser what mime type file is
  78. #
  79.  
  80. print $q->header("text/html");
  81.  
  82. print qq`
  83. <html>
  84. <head>
  85. </head>
  86. <body bgcolor="FFCC33">
  87. `;
  88.  
  89. print qq`
  90. <h2>Received messages</h2>
  91. `;
  92.  
  93. &print_table("Read Messages Per Day", "read_msg_avg_day");
  94.  
  95. print qq`
  96. <h2>Sent messages</h2>
  97. `;
  98.  
  99. &print_table("Sent Messages Per Day", "sent_msg_avg_day");
  100.  
  101. print qq`
  102. <h2>Usage in Minutes</h2>
  103. `;
  104.  
  105. &print_table("Average Usage Per Day", "usage_avg_day");
  106.  
  107. print qq`
  108. </body>
  109. </html>
  110. `;
  111.